package gui;

import gui.components.EditorPanel;
import gui.listeners.MainWindowMenuListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

public class MainWindow extends JFrame{
  private static final long serialVersionUID = 1L;
  
  private MenuBar menu;
  private Menu fileMenu;
  private MenuItem loadPhotosMenuItem;
  private MenuItem newProjectMenuItem;
  
  private JPanel mainPanel;
  private JTabbedPane tabbedPane;
  
  private MainWindowMenuListener menuListener;

	public MainWindow(){
		initialize();
	}
	
	private void initialize(){
		setSize(800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setMenuBar(getMenu());
		getContentPane().add(getMainPanel());
		setTitle("FotoArt HD");
	}
	
	private MenuBar getMenu(){
		if(menu == null){
			menu = new MenuBar();
			menu.add(getFileMenu());
		}
		return menu;
	}
	
	private Menu getFileMenu(){
		if(fileMenu == null){
			fileMenu = new Menu("Project");
			fileMenu.add(getNewProjectMenuItem());
			fileMenu.add(getLoadPhotosMenuItem());
		}
		return fileMenu;
	}
	
	private MenuItem getNewProjectMenuItem(){
		if(newProjectMenuItem == null){
			newProjectMenuItem = new MenuItem("New Project...");
			newProjectMenuItem.setEnabled(false);
		}
		return newProjectMenuItem;
	}
	
	private MenuItem getLoadPhotosMenuItem(){
		if(loadPhotosMenuItem == null){
			loadPhotosMenuItem = new MenuItem("Add Photos...");
			loadPhotosMenuItem.setActionCommand(MainWindowMenuListener.ACTION_LOAD_PHOTOS);
			loadPhotosMenuItem.addActionListener(getMenuListener());
		}
		return loadPhotosMenuItem;
	}
	
	private ActionListener getMenuListener(){
		if(menuListener == null){
			menuListener = new MainWindowMenuListener(this);
		}
		return menuListener;
	}
	
	private JPanel getMainPanel(){
		if(mainPanel == null){
		  mainPanel = new JPanel(new BorderLayout());
		  mainPanel.setBorder(new CompoundBorder(new MatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY), new EmptyBorder(5, 5, 5, 5)));
		  mainPanel.add(getTabbedPane(), BorderLayout.CENTER);
		}
		return mainPanel;
	}
	
	private JTabbedPane getTabbedPane(){
		if(tabbedPane == null){
			tabbedPane = new JTabbedPane();
			tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
			tabbedPane.add("New", new EditorPanel());
		}
		return tabbedPane;
	}
	
	public EditorPanel getCurrentEditor(){
		return (EditorPanel)getTabbedPane().getSelectedComponent();
	}
}
